package com.szqhyxj.youxiangju.biz.equipmentActivation.equipmentCardslot;


import android.util.Log;

import com.alibaba.fastjson.JSONArray;
import com.szqhyxj.youxiangju.biz.merchantHome.deviceinfo.DeviceInfo;
import com.szqhyxj.youxiangju.biz.merchantHome.deviceinfo.DeviceModel;
import com.szqhyxj.youxiangju.sys.BaseResult;
import com.szqhyxj.youxiangju.sys.components.androidpicker.qqtheme.framework.util.ConvertUtils;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by leung on 2018/11/3.
 */

public class EquipmentCardSlotPresenter implements EquipmentCardSlotContract.Presenter {
    EquipmentCardSlotContract.View view;
    EquipmentCardSlotModel.EquipmentCardSlotEntity cardSlotEntity;

    public EquipmentCardSlotPresenter(EquipmentCardSlotContract.View view) {
        this.view = view;
        cardSlotEntity = new EquipmentCardSlotModel.EquipmentCardSlotEntity();
    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void getDeviceInfo() {
        EquipmentCardSlotModel.getdeviceinfo()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseResult<EquipmentCardSlotModel.EquipmentCardSlotEntity>>() {
                    @Override
                    public void accept(BaseResult<EquipmentCardSlotModel.EquipmentCardSlotEntity> equipmentCardSlotEntityBaseResult) throws Exception {
                        if (equipmentCardSlotEntityBaseResult.businessLogicSuccess()) {
                            view.showAddress(equipmentCardSlotEntityBaseResult.getData().getAreaInfo());
                            view.showDeviceNo(equipmentCardSlotEntityBaseResult.getData().getDevNo());
                            cardSlotEntity.setAreaInfo(equipmentCardSlotEntityBaseResult.getData().getAreaInfo());
                            cardSlotEntity.setDevNo(equipmentCardSlotEntityBaseResult.getData().getDevNo());
                            view.showList();
                        } else {
                            view.showToast(equipmentCardSlotEntityBaseResult.getMessage());
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.showToast(throwable.getMessage());
                    }
                });
    }

    @Override
    public void getSlotList(String devno) {
        ArrayList<SlotBean> slotBeans = new ArrayList<>();
        slotBeans.clear();

        EquipmentCardSlotModel.getCardSlotinfo(devno)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseResult<EquipmentCardSlotModel.EquipmentSlotEntity>>() {
                    @Override
                    public void accept(BaseResult<EquipmentCardSlotModel.EquipmentSlotEntity> equipmentSlotEntityBaseResult) throws Exception {

                        if (equipmentSlotEntityBaseResult.businessLogicSuccess()) {
                            int portNum = equipmentSlotEntityBaseResult.getData().getSurplusPort();
                            view.showDeviceicon(equipmentSlotEntityBaseResult.getData().getBrandType());
                            Map<String, String> map = equipmentSlotEntityBaseResult.getData().getPosDevicePorts();
                            Log.e("12345678", map + "");

                            Iterator<String> it = map.keySet().iterator();  //map.keySet()得到的是set集合，可以使用迭代器遍历
                            while (it.hasNext()) {
                                String key = it.next();
                                System.out.println("key值：" + key + " value值：" + map.get(key));
                                SlotBean slotBean = new SlotBean();
                                slotBean.setSlot(key);
                                slotBean.setCardno(map.get(key));
                                slotBeans.add(slotBean);
                            }

                            view.showSlotList(slotBeans);
                        } else {
                            view.showToast(equipmentSlotEntityBaseResult.getMessage());
                        }


                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.showToast(throwable.getMessage());
                    }
                });

    }

    @Override
    public void setdeviceinfo() {
        DeviceModel.getdeviceinfo()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult<DeviceModel.DeviceInfoBean>>() {
                    @Override
                    public void accept(BaseResult<DeviceModel.DeviceInfoBean> deviceInfoBaseResult) throws Exception {
                        if (deviceInfoBaseResult.businessLogicSuccess()) {
                            DeviceModel.DeviceInfoBean deviceInfoBean = deviceInfoBaseResult.getData();
                            DeviceInfo.Holder.getInstance().setAreaInfo(deviceInfoBean.getAreaInfo());
                            DeviceInfo.Holder.getInstance().setDevNo(deviceInfoBean.getDevNo());
                            DeviceInfo.Holder.getInstance().setAepnType(deviceInfoBean.getAepnType());
                            DeviceInfo.Holder.getInstance().setLocalVersion(deviceInfoBean.getLocalVersion());
                            DeviceInfo.Holder.getInstance().setBattery(deviceInfoBean.getBattery());
                            DeviceInfo.Holder.getInstance().setUpdateDate(deviceInfoBean.getUpdateDate());
                            DeviceInfo.Holder.getInstance().setOnlineState(deviceInfoBean.getOnlineState());
                            view.deviceinit();

                        } else {
                            view.showToast(deviceInfoBaseResult.getMessage() + "");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.showToast(throwable.getMessage() + "");
                    }
                });
    }
}
